const MAX_LIMIT = 15

Page({

    /**
     * 页面的初始数据
     */
    data: {
        swiperImgUrls: [],
        playlist: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this._getPlaylist()
        this._getSwiperImgs()
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        this.setData({
            playlist:[]
        })
        this._getPlaylist()
        this._getSwiperImgs()
    },

    // 获取歌单列表
    _getPlaylist() {
        wx.showLoading({
          title: '加载中',
        })

        wx.cloud.callFunction({
            name: 'music',
            data: {
                start: this.data.playlist.length,
                count: MAX_LIMIT,
                $url: 'playlist'   
            }
        }).then(res => {
            console.log('歌单列表', res.result.data)
            this.setData({
                playlist: this.data.playlist.concat(res.result.data)
            })
            wx.stopPullDownRefresh()
            wx.hideLoading()
        })
    },

    // 获取轮播图数据
    _getSwiperImgs() {
        wx.cloud.database().collection('swiper').get().then(res => {
            this.setData({
                swiperImgUrls: res.data
            })
        })
    }
})